<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Clubs;
use DB,View,Validator;

class ClubController extends Controller
{
    //

    public function listshow()
    {
    	$clubs = DB::table('clubs')->get();
    	return view()->make('admin.clubs.list')->with(['clubs'=>$clubs]);
    }

    public function create()
    {
    	return view()->make('admin.clubs.create');
    }

    public function edit(Request $request)
    {
    	$club = Clubs::where('id', $request->input('id'))->firstOrFail();
    	return view()->make('admin.clubs.edit')->with(['id'=>$club->id, 'club'=>$club]);
    }

    public function update(Request $request)
    {
    	$validator = Validator::make($request->all(), [
            'name' => 'required|min:2|max:24',
            'type' => 'required',
        ]);
        if($validator->fails()){
            return redirect()->back()->withErrors($validator)->withInput();
        }

        $rs = Clubs::where('id', $request->input('id'))->first()->update($request->all());
        #Todo 返回之前页面

        return redirect(route('clubs.list'));
    }

    public function insert(Request $request)
    {
    	$validator = Validator::make($request->all(), [
            'name' => 'required|min:2|max:24',
            'type' => 'required',
        ]);

    	if($validator->fails()){
    		return redirect(route('clubs.create'))->withErrors($validator)->withInput();
    	}

		$rs = Clubs::create($request->all());
        #Todo 返回之前页面
        return redirect(route('clubs.list'));
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        $rs = DB::table('clubs')->where('id', $id)->delete();
        if($rs){
            $notice = "删除成功";
        }else{
            $notice = "删除失败";
        }
    	return redirect(route('clubs.list'))->with(['notice'=>$notice]);
    }
}
